package com.bawei.bydayzg_two

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil.setContentView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.base.mvvm.BaseActivity
import com.bawei.bydayzg_two.adapter.CateONeAdapter
import com.bawei.bydayzg_two.adapter.CateTwoAdapter
import com.bawei.bydayzg_two.databinding.ActivityCateBinding
import com.bawei.bydayzg_two.viewModel.CartGroyViewModel
import com.blankj.utilcode.util.ToastUtils

@Route(path = "/app/CateActivity")
class CateActivity : BaseActivity<ActivityCateBinding,CartGroyViewModel>() {
    lateinit var cateONeAdapter: CateONeAdapter
    lateinit var cateTwoAdapter: CateTwoAdapter
    //
    override fun getLoutyId(): Int {
        return R.layout.activity_cate
    }

    override fun getViewModel(): Class<CartGroyViewModel> {
        return CartGroyViewModel::class.java
    }

    override fun initView() {

        //
        cateONeAdapter = CateONeAdapter()
        binding.rvCate.adapter = cateONeAdapter
        binding.rvCate.layoutManager = LinearLayoutManager(this)
        //
        cateTwoAdapter = CateTwoAdapter()
        binding.rvCateTwo.adapter = cateTwoAdapter
        val linearLayoutManager = LinearLayoutManager(this)
        binding.rvCateTwo.layoutManager = linearLayoutManager
        //
        viewModel.cartGroy()

        //点击左侧分类，右侧分类名称一致的置顶c
        cateONeAdapter.setOnItemClickListener { adapter, view, position ->
//            binding.rvCateTwo.scrollToPosition(position)
            linearLayoutManager.scrollToPositionWithOffset(position,0)
            //
            cateONeAdapter.getIndex(cateONeAdapter.data[position].id)
        }

        //滑动右侧，当两边显示不一致时，让他显示一直的
        binding.rvCateTwo.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            //找到第一个显示的
            val position = linearLayoutManager.findFirstVisibleItemPosition()
            //
            if (cateTwoAdapter.data[position].id != cateONeAdapter.index){
                cateONeAdapter.getIndex(cateTwoAdapter.data[position].id)
            }
        }
        //点击跳转对应的详情页面
    }

    override fun initDate() {

        //
        viewModel.cartGroySuccess.observe(this){
            if (it.code == 200){
                cateONeAdapter.data.clear()
                cateONeAdapter.data.addAll(it.data)
                cateONeAdapter.notifyDataSetChanged()

                //
                cateTwoAdapter.data.clear()
                cateTwoAdapter.data.addAll(it.data)
                cateTwoAdapter.notifyDataSetChanged()
            }
            //
            ToastUtils.showLong(it.message)
        }

    }
}